<?php
/**
 * 管理中心· 插件中心管理模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
class PluginController extends BaseController {
    //初始化程序
    public function _initialize()
    {
        parent::_initialize();
    
        $this->assign('pwds',md5('unite1688'));
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取插件列表的功能
     * @author:XC跃
     * @date :2018-03-16
     +--------------------------------------------------------------------------
     */
    public function plugList(){
        //实例化相关表
        $list = M('plugin')->where('1')->select();
        $this->assign("list",$list);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现添加(修改)插件的功能
     * @author:XC跃
     * @date :2018-03-16
     +--------------------------------------------------------------------------
     */
    public function plugAddEdit(){
        $id = I('id/d');
        //实例化相关表
        $plident = M('plugin');
        
        if(IS_POST){
            $data = I('post.');
            //dump($data);
            $pdata['pl_name'] = $data['pname'];
            $pdata['pl_code'] = $data['pcode'];
            $pdata['pl_state']= $data['state'];
            $pdata['pl_img']  = $data['pimg'];
            $pdata['pl_conf'] = $this->plugDataHand($data);
            if(''!=$id){
                $re = $plident->where("`pl_id`='".$id."'")->save($pdata);
            }else{
                $re = $plident->add($pdata);
            }
            if($re>0){
                $res = array('status'=>1,'msg'=>'配置插件数据保存成功');
                exit(json_encode($res));
            }
        }else{
            if(''!=$id){
                $re = $plident->where("`pl_id`='".$id."'")->find();
                if(count($re)=='0') layer_error('参数错误！');
                $this->assign("re",$re);
                $this->assign("reForm",unserialize($re['pl_conf']));
            }
            $this->display();
        }
    }
    /**
     +--------------------------------------------------------------------------
     * 实现添加(修改)插件的表单模块的数据处理
     * @author:XC跃
     * @date :2018-03-16
     +--------------------------------------------------------------------------
     */
    public function plugDataHand($data){
        //接收值
        $foType = $data['fo_type'];
        $foTitle= $data['fo_title'];
        $foName = $data['fo_name'];
        $foArea = $data['fo_area'];
        //P($foArea);
        //数据处理
        $arr = array();
        foreach($foType as $k=>$v){
            if($foType[$k]=='' || $foTitle[$k]=='' || $foName[$k]==''){
                $res = array('status'=>-1,'msg'=>'请把配置填写规范');
                exit(json_encode($res));
            }
            $arr[$k]['fo_type'] = $foType[$k];
            $arr[$k]['fo_title']= $foTitle[$k];
            $arr[$k]['fo_name'] = $foName[$k];
            if($foType[$k] == 'select'){
                unset($option);
                $item = explode(",", strFilter($foArea[$k]));  //过滤特殊字符并转成数组
                //P($item);
                foreach ($item as $key => $val){
                    if(empty($val)){
                        unset($item[$key]);
                    }else{
                        $option[] = $val;
                        $arr[$k]['fo_area']  = $option;
                    }
                }
            }
            if($foType[$k] == 'radio'){
                unset($radio);
                $item = explode(",", strFilter($foArea[$k]));  //过滤特殊字符并转成数组
                foreach ($item as $key => $val){
                    if(empty($val)){
                        unset($item[$key]);
                    }else{
                        $radio[] = $val;
                        $arr[$k]['fo_area']  = $radio;
                    }
                }
            }
        }
        //dump($arr);
        $config = serialize($arr);
        return $config;
    }
    /**
     +--------------------------------------------------------------------------
     * 实现相关参数配置
     * @author:XC跃
     * @date :2018-03-16
     +--------------------------------------------------------------------------
     */    
    public function plugSet(){
        $id = I('id/d');
        //实例化相关表
        $plident = M('plugin');
        if(IS_POST){
            $config = I('post.config/a');
            if($config){
                $config = serialize($config);
            }
            $row = $plident->where("`pl_id`='".$id."'")->save(array('pl_value'=>$config));
            if($row>0){
                $res = array('status'=>1,'msg'=>'参数配置成功');
            }else{
                $res = array('status'=>-1,'msg'=>'未做更改');
            }exit(json_encode($res));
        }else{
            $re = $plident->where("`pl_id`='".$id."' and `pl_state`=1")->find();
            if(count($re)=='0') layer_error('参数错误！');
            $this->assign("re",$re);
            //P(unserialize($re['pl_conf']));
            $this->assign("reForm",unserialize($re['pl_conf']));
            $this->assign('reValue',unserialize($re['pl_value']));
            
            $this->display();
        }
    }
}